<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 权限操作功能
 * @package  htdocs
 * @subpackage  quanliadmin
 */

quanli_require('module/base/project/permission.class.php');

class pa_permission {
	
	public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //权限列表页
                    $this->searchPermissionList();
                    break;

                case 2: //载入权限
                    $this->reloadPermission();
                    break;

                default:
                    backendError();
                    break;
            }

        } else {
            backendError();
        }

	}
	
	//权限列表页
	private function searchPermissionList() {

		global $G_PGLOB;

        //实例权限类
		$permissionObj = new permission( $G_PGLOB->DBCA );

        //获得权限数组
        $permission_group_array = $permissionObj->searchPermissionGroup();
		
		$G_PGLOB->smarty->assign_by_ref('permission_group_array', $permission_group_array);
		
		$G_PGLOB->smarty->display('project/permissionList.tpl');

	}

    /**
     * 载入权限
     */
    private function reloadPermission() {

        global $G_PGLOB;

        $G_PGLOB->sessionCache_remove( 'PS_PERMISSIONEDURI' , true );

        //查询用户权限
        $search_sql = 'SELECT distinct p.permissionID,p.permissionRequest FROM user_permission p
                       INNER JOIN user_rolepermission rp ON p.permissionID=rp.permissionID
                       INNER JOIN user_userrole ur ON ur.roleID=rp.roleID AND ur.userID=?';
        $result_permissions = $G_PGLOB->DBCA->stmt_query($search_sql, 's', $G_PGLOB->currentUserID);
        if (is_null($result_permissions))
            $this->toError('当前帐户未做系统初始化');

        foreach ($result_permissions as $permission) {
            $USER_PERMISSIONS[$permission->permissionID] = $permission->permissionRequest;
        }

        $PS_LOGIN_SESSION = $G_PGLOB->sessionCache_get ( 'PS_LOGIN_SESSION' );
        $PS_LOGIN_SESSION['USER_PERMISSIONS'] = $USER_PERMISSIONS;
        $G_PGLOB->sessionCache_set('PS_LOGIN_SESSION', $PS_LOGIN_SESSION, G_LOGINSESSION_TIMEOUT);

        header('location:/quanliadmin/index.php');

    }

}